/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package net.codejava.mail;

import com.sun.mail.pop3.POP3Store;
import static java.lang.System.out;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;

/**
 *
 * @author Lucas Nguyen
 */
public class NewClass {
    
   public static void main(String[] args) {

		String mailPop3Host = "nhom5.no-ip.org";
		String mailStoreType = "pop3";
		String mailUser = "hoangthach@nhom5.no-ip.org";
		String mailPassword = "1";
                
                try {
                         Properties properties = new Properties();
			properties.put("mail.pop3.host", mailPop3Host);
			Session emailSession = Session.getDefaultInstance(properties);

			POP3Store emailStore = (POP3Store) emailSession.getStore(mailStoreType);
                                
                        emailStore.connect(mailPop3Host, mailUser,mailPassword);
                        out.println("Kết nối thành công...");
                        
                        Folder emailFolder = emailStore.getFolder("INBOX");
                        emailFolder.open(Folder.READ_ONLY);
                        out.println("Đang nạp hộp thư đến...");
                        
                        Message[] messages = emailFolder.getMessages();
                        System.out.print(messages.length);
                        for (int i = 0; i < messages.length; i++) {
				Message message = messages[i];
				System.out.println("==============================");
				System.out.println("Email #" + (i + 1));
				System.out.println("Subject: " + message.getSubject());
				System.out.println("From: " + message.getFrom()[0]);
				System.out.println("Text: " + message.getContent().toString());
                                 System.out.println("Text: " + message.getDisposition());
                                 System.out.println("Ngày gữi: " + message.getSentDate().toString());
                                 System.out.println("Kích thước: " + message.getSize());
                                System.out.println(message.isSet(Flags.Flag.SEEN) ? "Đã đọc" : "Chưa đọc");
                                
                                 
			}
                        emailFolder.close(false);
			emailStore.close();
        } catch (Exception e) {
             e.printStackTrace();
        }
                
    }
    
}
